package com.eleadmin.common.system.controller;

import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.system.service.UserAddressService;
import com.eleadmin.common.system.entity.UserAddress;
import com.eleadmin.common.system.param.UserAddressParam;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.BatchParam;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 控制器
 *
 * @author EleAdmin credit_rating_score
 * @since 2025-04-27 23:27:57
 */
@Api(tags = "管理")
@RestController
@RequestMapping("/system/userAddress")
public class UserAddressController extends BaseController {
    @Resource
    private UserAddressService userAddressService;

    @Resource
    private UserService userService;


    @OperationLog
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ApiResult<PageResult<UserAddress>> page(UserAddressParam param) {
        PageResult<UserAddress> pageResult = userAddressService.pageRel(param);
        pageResult.getList().forEach(userAddress -> {
            userAddress.setUserName(userService.getById(userAddress.getUserId()).getNickname());
        });
        return success(pageResult);

    }


    @OperationLog
    @ApiOperation("查询全部")
    @GetMapping()
    public ApiResult<List<UserAddress>> list(UserAddressParam param) {
        param.setUserId(getLoginUserId());

        return success(userAddressService.listRel(param));

    }


    @OperationLog
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public ApiResult<UserAddress> get(@PathVariable("id") Integer id) {
        return success(userAddressService.getById(id));

    }


    @OperationLog
    @ApiOperation("添加")
    @PostMapping()
    public ApiResult<UserAddress> save(@RequestBody UserAddress userAddress) {
        userAddress.setUserId(getLoginUserId());
        if (userAddress.getId() ==null){
            if (userAddressService.save(userAddress)) {
                return success(userAddress);
            }
        }else {
            if (userAddressService.updateById(userAddress)) {
                return success(userAddress);
            }
        }

        return fail(userAddress);
    }


    @OperationLog
    @ApiOperation("修改")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserAddress userAddress) {
        if (userAddressService.updateById(userAddress)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @OperationLog
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (userAddressService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }


    @OperationLog
    @ApiOperation("批量添加")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<UserAddress> list) {
        if (userAddressService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }


    @OperationLog
    @ApiOperation("批量修改")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<UserAddress> batchParam) {
        if (batchParam.update(userAddressService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @OperationLog
    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (userAddressService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
